package com.guigui.erp.controller;

import com.guigu.erp.dto.ManufactureApplyDto;
import com.guigu.erp.pojo.ManufactureApply;
import com.guigu.erp.service.ManufactureApplyService;
import com.guigu.erp.utils.PageUtils;
import com.guigu.erp.vo.DesignFileVo;
import com.guigui.erp.utils.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author Lenovo
 */
@Controller
@RequestMapping("/procedure/apply")
public class ManufactureApplyController {
    @Autowired
    private NumberUtils numberUtils;

    @Autowired
    private ManufactureApplyService manufactureApplyService;

    @RequestMapping("/add")
    @ResponseBody
    public boolean addManufactureApply(@RequestBody ManufactureApplyDto manufactureApplyDto) {
        boolean flag = false;
        // 生成安全库存配置单编号
        String applyId = numberUtils.getTypeNumber("300");
        manufactureApplyDto.setApplyId(applyId);
        List<ManufactureApply> details = manufactureApplyDto.getDetails();
        for (int i = 0; i < details.size(); i++) {
            ManufactureApply manufactureApply = details.get(i);
            manufactureApply.setId(null);
            manufactureApply.setRegister(manufactureApplyDto.getRegister());
            manufactureApply.setRegisterTime(manufactureApplyDto.getRegisterTime());
            manufactureApply.setApplyId(applyId);
            manufactureApply.setCheckTag("S001-0");
            manufactureApply.setManufactureTag("P001-0");
            flag = manufactureApplyService.save(manufactureApply);
        }
        return flag;
    }

    /**
     * 库存安全配置单的复核查询
     */
    @RequestMapping("/manufactureApplyQuery")
    @ResponseBody
    public PageUtils<ManufactureApply> recheckPage(DesignFileVo vo, int pageNo, int pageSize) {
        PageUtils<ManufactureApply> recheckPage = this.manufactureApplyService.recheckPage(vo, pageNo, pageSize);
        return recheckPage;
    }

    /**
     * 根据id查询生产计划单信息
     */
    @RequestMapping("/manufactureApplyGetId")
    @ResponseBody
    public ManufactureApply manufactureApplyGetId(int id) {
        ManufactureApply manufactureApply = this.manufactureApplyService.getById(id);
        return manufactureApply;
    }

    /**
     * 生产计划单复核通过
     * @param
     * @return
     */
    @RequestMapping("manufactureApplyReviewPassed")
    @ResponseBody
    public String manufactureApplyReviewPassed(ManufactureApply manufactureApply) {
        manufactureApply.setCheckTag("S001-1");
        boolean flag = manufactureApplyService.updateById(manufactureApply);
        if (flag) {
            return "correct";
        }
        return "error";
    }

    /**
     * 生产计划单查询请求
     */
    @RequestMapping("/list")
    @ResponseBody
    public PageUtils<ManufactureApply> manufactureApplyList(DesignFileVo vo, int pageNo, int pageSize) {
        PageUtils<ManufactureApply> page = this.manufactureApplyService.manufactureApplyListPage(vo, pageNo, pageSize);
        return page;
    }

    /**
     * 生产派工单的未派工查询请求
     */
    @RequestMapping("/TooList")
    @ResponseBody
    public PageUtils<ManufactureApply> manufactureApplyList2(DesignFileVo vo, int pageNo, int pageSize) {
        PageUtils<ManufactureApply> page = this.manufactureApplyService.manufactureApplyListPage2(vo, pageNo, pageSize);
        return page;
    }
}
